package com.dp.appi.controller;

import com.dp.appi.exception.CommonException;
import com.dp.appi.service.ChannelService;
import com.dp.appi.service.ExplorationService;
import com.dp.appi.service.PTService;
import com.dp.appi.service.StageService;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.SYS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * Created by bchen on 2016/5/19.
 */
@Controller
@RequestMapping(value = "/channelControl")
public class ChannelControl {
    private static Logger log = LoggerFactory.getLogger(ChannelControl.class);

    @Autowired
    private ChannelService channelService;

    @Autowired
    private ExplorationService explorationService;

    @Autowired
    private StageService stageService;

    @Autowired
    private PTService ptService;

    /**
     * 通道特征点(有下级)列表
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/channelAList",method = RequestMethod.GET)
    public void ptList(ModelMap modelMap, @RequestParam Map<String,String> param) {
        try {
            modelMap.addAttribute("channelAList",channelService.channelAList(Long.parseLong(param.get("exploration_id"))));
        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }


    /**
     * 通道特征点(无下级)列表
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/channelList",method = RequestMethod.GET)
    public void channelList(ModelMap modelMap, @RequestParam Map<String,String> param) {
        try {
            modelMap.addAttribute("channelList",channelService.channelList(Long.parseLong(param.get("exploration_id"))));
        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     *  关联通道特征信息
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/corrChannelFeature",method = RequestMethod.POST)
    public void corrChannelFeature(ModelMap modelMap, @RequestParam Map<String,String> param) {
        try {
            Long exploration_id = Long.parseLong(param.get("exploration_id"));
            Long after_exp = Long.parseLong(param.get("after_exp"));

            //定位点和特征点不存在，特征点已关联，则抛出异常
            if(!explorationService.isExistFeature(exploration_id,12)){
                throw new CommonException("EXPO007");
            }else if(!explorationService.isExistFeature(after_exp,11)){
                throw new CommonException("EXPO008");
            }else if(explorationService.isCorrFeature(exploration_id)){
                throw new CommonException("EXPO009");
            }else {
                modelMap.addAttribute("channel_id",channelService.corrChannelFeature(Long.parseLong(param.get("exploration_id")),Long.parseLong(param.get("after_exp"))));
            }
        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch(Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     *  新增通道特征信息
     * @param modelMap
     * @param param
     * 特征点类型: pt_id
     * 下级级联：after_exp
     * 距离上联距离：distance
     */
    @RequestMapping(value = "/addChannelFeature",method = RequestMethod.POST)
    public void addChannelFeature(ModelMap modelMap, @RequestParam Map<String,String> param) {
        try {
            int pt_id = Integer.parseInt(param.get("pt_id"));
            Long after_exp = Long.parseLong(param.get("after_exp"));
            float distance = Float.parseFloat(param.get("distance")==null?"0":param.get("distance"));
            Map<String,Object> expMap= explorationService.explorBaseInfo(after_exp);
            String number = stageService.newExpNum(Long.parseLong(String.valueOf(expMap.get("stage_id"))),pt_id);
            expMap.put("number",number);
            modelMap.addAttribute("channel_id",channelService.addChannelFeature(pt_id,distance,expMap));

        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch(Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            e.printStackTrace();
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 查询特征点扩展信息
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/queryChannelFeature",method = RequestMethod.GET)
    public void queryChannelFeature(ModelMap modelMap, @RequestParam Map<String,String> param) {
        try {
            modelMap.addAttribute("pt",channelService.queryChannelFeature(Long.parseLong(param.get("channel_id"))));
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 修改通道特征信息
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/upChannelFeature",method = RequestMethod.POST)
    public void upChannelFeature(ModelMap modelMap, @RequestParam Map<String,Object> param) {
        try {
            Map<String,Object> expMap = explorationService.explorBaseInfoByChannelID(Long.parseLong(String.valueOf(param.get("channel_id"))));
            if(null == expMap){
                throw new CommonException("EXPO010");
            }
            if(null == expMap.get("pt_objectid")){
                ptService.newPt(param, Integer.parseInt(String.valueOf(expMap.get("pt_id"))),Long.valueOf(String.valueOf(expMap.get("exploration_id"))));
            }else {
                ptService.upPt(param, Integer.parseInt(String.valueOf(expMap.get("pt_id"))), Long.parseLong(String.valueOf(expMap.get("pt_objectid"))));
            }
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 修改通道特征点距离上级距离
     * @param modelMap
     * @param exploration_id 特征点id
     * @param distance
     */
    @RequestMapping(value = "/upExpDistance",method = RequestMethod.POST)
    public void upExpDistance(ModelMap modelMap, @RequestParam Float distance,@RequestParam Long exploration_id) {
        try {
            explorationService.upExpDistance(exploration_id,distance);
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }


    /**
     * 删除通道特征信息
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/delChannelFeature",method = RequestMethod.POST)
    public void delChannelFeature(ModelMap modelMap, @RequestParam Map<String,Object> param) {
        try {
            Long channelId = Long.valueOf(String.valueOf(param.get("channel_id")));
           channelService.delChannelFeature(channelId);
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }


}
